package com.example.hello;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class sportlist extends AppCompatActivity implements AdapterView.OnItemClickListener{
ListView listview;
sp_adapter adapter;
    String bt;
    String sp_title;
    String timeStr;
    TextView total_num,sp_name,sp_num;
    private static final String TAG = "sportlist";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_sportlist);
        listview = findViewById(R.id.sport);
        listview.setOnItemClickListener(this);
        listview.setTextFilterEnabled(true);
sp_name = findViewById(R.id.sp_name);
sp_num = findViewById(R.id.sp_num);
total_num = findViewById(R.id.total_num);
        Handler handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if (msg.what == 8) {

                    ArrayList<HashMap<String,String>> splist = (ArrayList<HashMap<String, String>>) msg.obj;
                  adapter =new sp_adapter(sportlist.this,R.layout.sp_list,splist);
                    listview.setAdapter(adapter);
                    listview.setVisibility(View.VISIBLE);

                }
                super.handleMessage(msg);
            }

        };
        sporttask task = new sporttask();
        task.setHandler(handler);
        Thread t = new Thread(task);
        t.start();

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final EditText sp_edt = new EditText(this);

        sp_edt.setMinLines(3);
        new AlertDialog.Builder(this)
                .setTitle("请输入运动量(单位/小时)")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setView(sp_edt)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        TextView title = (TextView) view.findViewById(R.id.sportTitle);
                        TextView detail = (TextView) view.findViewById(R.id.sportDetail);
                        sp_title = String.valueOf(title.getText());
                        String sp_detail = String.valueOf(detail.getText());
                        sp_detail =sp_detail.substring(0,3);
                        Log.i(TAG, "onItemClick: title2="+sp_title);
                        Log.i(TAG, "onItemClick: detail2="+sp_detail);
                        Float sp_number =Float.parseFloat(String.valueOf(sp_edt.getText()));
                        Float sp_standard = Float.parseFloat(sp_detail);
                        sp_name.setText(sp_title);
                        sp_num.setText(String.valueOf(sp_number)+"小时");
                        DecimalFormat df=new DecimalFormat(".##");
                        Float total = sp_number*sp_standard;
                         bt=df.format(total);
                        total_num.setText(bt+"卡路里");
                        SharedPreferences sharedPreferences = getSharedPreferences("sport", Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putFloat("sp_number",sp_number);
                        editor.putString("sp_title",sp_title);
                        editor.putFloat("sp_detail",sp_standard);
                        editor.putString("sp_total",bt);
                        //保存
                        editor.commit();
                        Log.i(TAG,"onActivityResult:数据已保存");

                    }
                })
                .setNegativeButton("取消", null)
                .show();

    }
    public void ret_sp(View btn) {

        Intent config = new Intent(this, MainActivity.class);
        startActivity(config);//请求代码
        Log.i("open", "openMainactivity: ");
       SportItem item = new SportItem(sp_title,bt);
        listManager manager = new listManager(this);
        SharedPreferences time_sharedPreference = getSharedPreferences("message", Activity.MODE_PRIVATE);
        timeStr = time_sharedPreference.getString("time","");//设置时间变量
        Log.i(TAG, "onCreate: timeStr="+timeStr);
        Date date = new Date();
        SimpleDateFormat DF = new SimpleDateFormat("dd-MM-yyyy");//获取本次访问时间
        if(timeStr.equals(DF.format(date))) {
            manager.sp_add(item);
            List<SportItem> testList = manager.sp_listAll();
            for(SportItem i :testList){
                Log.i(TAG,"测试数据库"+i.getCurName()+i.getCurSport());}
        }
        else{
            manager.deleteAll();
        }

    }

}